import { ShelfCourse } from '@/api/Shelf';
import { useReactive } from 'ahooks';
import { App, Drawer } from 'antd';
import { useState } from 'react';
import type { YmColumnsType } from 'ym-design';
import { YmTable } from 'ym-design';
import { CourseDetail, CourseDetailProps } from '../Detail';

type RecordType = ShelfCourse;

export function List() {
  const [columns] = useState<YmColumnsType<RecordType>>([
    {
      title: '名称',
      dataIndex: 'name',
      key: 'name',
      width: 200,
    },
    {
      title: '修改时间',
      dataIndex: 'updateTime',
      key: 'updateTime',
      width: 300,
    },
    {
      title: '学习对象',
      dataIndex: 'learnObjText',
      key: 'learnObjText',
      width: 300,
    },
    {
      title: '数量',
      dataIndex: 'num',
      key: 'num',
      width: 200,
    },
    {
      key: '_operate',
      title: '操作',
      width: 100,
    },
  ]);
  const [tableDep, _reFreshTable] = useState<number>();
  const reFreshTable = () => _reFreshTable(Math.random());
  const { modal } = App.useApp();
  const detailsPorps = useReactive<{ props?: CourseDetailProps }>({});

  return (
    <div className="safe_boss_page">
      <YmTable
        searchForm={{
          formItems: [
            {
              label: '名称',
              name: 'name',
              valueType: {
                type: 'input',
              },
            },
          ],
          span: 6,
        }}
        columns={columns}
        request={async ({ page }, form = {}) => {
          const { current = 0, pageSize = 10 } = page ?? {};
          const data = await ShelfCourse.onShelfList(current, pageSize, form);
          console.log(data, '----为啥没有结果');
          return data;
        }}
        requestOptions={{
          reFreshDep: [tableDep],
          defaultPageSize: 20,
        }}
        rowKey="guid"
        operate={[
          {
            text: '详情',
            key: 'detail',
            onClick(record) {
              detailsPorps.props = {
                guid: record.guid,
                name: record.name,
                createTime: record.createTime,
                shelfInfo: {
                  isChargeText: record.isChargeText,
                  learnObjText: record.learnObjText,
                },
              };
            },
          },
          {
            text: '下架',
            key: 'listing',
            onClick(record) {
              modal.confirm({
                title: '提示',
                content: `确定要下架【${record.name}】吗?`,
                async onOk() {
                  await ShelfCourse.shelf.down(record.guid);
                  reFreshTable();
                },
              });
            },
          },
        ]}
      ></YmTable>
      <Drawer
        width={800}
        title="详情"
        open={Boolean(detailsPorps.props)}
        onClose={() => (detailsPorps.props = undefined)}
      >
        <CourseDetail {...detailsPorps.props} />
      </Drawer>
    </div>
  );
}
